package com.hua.store.web.servlet;

import com.hua.store.domin.*;
import com.hua.store.service.OrderService;
import com.hua.store.service.impl.OrderServiceImpl;
import com.hua.store.utils.UUIDUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@WebServlet(name = "OrderServlet", urlPatterns = "/OrderServlet")
public class OrderServlet extends BaseServlet {

    public String saveOrder(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //判断用户是否登录，如果登录则可以提交订单
        User user = (User) req.getSession().getAttribute("loginUser");
        if (user == null) {
            req.setAttribute("msg", "请登录后再提交");
            return "/jsp/login.jsp";
        }
        //从购物车中取出数据
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        Orders orders = new Orders();
        orders.setOid(UUIDUtils.getCode());
        orders.setOrdertime(new Date());
        orders.setTotal(cart.getTotal());
        orders.setState(1);
        orders.setUser(user);


        for (CartItem cartItem : cart.getCartItems()) {
            OrderItem orderItem = new OrderItem();
            orderItem.setItemid(UUIDUtils.getCode());
            orderItem.setQuantity(cartItem.getNum());
            orderItem.setTotal(cartItem.getSubTotal());
            orderItem.setProduct(cartItem.getProduct());
            orderItem.setOrder(orders);
            orders.getList().add(orderItem);
        }
        OrderService service = new OrderServiceImpl();
        service.saveOrder(orders);


        //清空购物车
        cart.clearCart();
        req.setAttribute("order", orders);


        return "/jsp/order_info.jsp";
    }

    public String findOrdersByUidWithPage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取当前用户
        User user = (User) req.getSession().getAttribute("loginUser");
        //获取当前页
        int curNum = Integer.parseInt(req.getParameter("num"));
        //
        OrderService service = new OrderServiceImpl();
        PageModel pm = service.findOrdersByUidWithPage(user, curNum);
        req.setAttribute("page", pm);
        return "/jsp/order_list.jsp";
    }

    //findOrderByOid
    public String findOrderByOid(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String oid = req.getParameter("oid");
        OrderService service = new OrderServiceImpl();
        Orders orders = service.findOrderByOid(oid);

        req.setAttribute("order", orders);
        return "/jsp/order_info.jsp";
    }

    //payOrder
    public String payOrder(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String address = req.getParameter("address");
        String name = req.getParameter("name");
        String telephone = req.getParameter("telephone");
        String oid = req.getParameter("oid");
        OrderService service = new OrderServiceImpl();
        Orders orders = service.findOrderByOid(oid);
        orders.setState(2);
        orders.setAddress(address);
        orders.setName(name);
        orders.setTelephone(telephone);
        service.updateOrder(orders);
        req.setAttribute("msg", "支付成功!");
        return "/jsp/test.jsp";
    }
}
